package com.mall.common.domain.content;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mall.common.domain.PriorityEntity;

/**
 * 文章分类实体
 */
@Entity
@Table(name="s_article_category")
@JsonIgnoreProperties(ignoreUnknown=true) 
public  class ArticleCategoryDomain extends PriorityEntity {

	private static final long serialVersionUID = 1L;
	private String name;                                      // 分类名称
	private String code;                                      // 分类编码
	private String mark; 										//路径标识
	private String metaTitle;                                 // 分类标识
	private String metaKeywords;                              // 关键词
	private String metaDescription;                           // 页面描述
	@JsonIgnore private ArticleCategoryDomain parent;                    // 上级分类
	private Boolean hasChild;                                 // 是否可以有子节点，取决于model类型
	@JsonIgnore private Set<ArticleCategoryDomain> children;             // 下级分类
	@JsonIgnore private Set<ArticleDomain> articleSet;                   // 文章
	private String template;	//文章模版
	private String url;
	
	@Transient
	public JSONObject getJSONObject(){
		JSONObject obj = new JSONObject();
		obj.put("id", this.getId());
		obj.put("pid", this.getParentId());
		obj.put("text", this.getName());
		return obj;
	}
	
	@PrePersist
	public void prePersist(){
		if(this.getPriority() == null){
			this.setPriority(10);
		}
	}
	
	@PreRemove
	public void preremove(){
		Set<ArticleCategoryDomain> children = getChildren();
		if (children != null) {
			for (ArticleCategoryDomain category : children){
				category.setParent(null);
			}
		}
		Set<ArticleDomain> articleSet = getArticleSet();
		if (articleSet != null) {
			for (ArticleDomain article : articleSet){
				article.setCategory(null);
			}
		}
	}
	
	@Transient
	public Long getParentId() { 
		ArticleCategoryDomain parent = getParent();
		if (parent != null) {
			return parent.getId();
		} else {
			return null;
		}
	}
	
	/**
	 * 默认根部门顶级分类
	 * id=顶级分类的id
	 * @return
	 */
	@Transient
	@JsonIgnore
	public static ArticleCategoryDomain genRoot(){
		ArticleCategoryDomain category = new ArticleCategoryDomain();
		category.setName("根栏目");
		return category;
	}
	
	@Column(nullable = false, length = 30) 
	public String getName() {
		return name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMetaTitle() {
		return metaTitle;
	}

	public void setMetaTitle(String metaTitle) {
		this.metaTitle = metaTitle;
	}

	public String getMetaKeywords() {
		return metaKeywords;
	}

	public void setMetaKeywords(String metaKeywords) {
		this.metaKeywords = metaKeywords;
	}

	public String getMetaDescription() {
		return metaDescription;
	}

	public void setMetaDescription(String metaDescription) {
		this.metaDescription = metaDescription;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "up_id")
	public ArticleCategoryDomain getParent() {
		return parent;
	}
	
	public void setParent(ArticleCategoryDomain parent) {
		this.parent = parent;
	}

	public Boolean getHasChild() {
		return hasChild;
	}

	public void setHasChild(Boolean hasChild) {
		this.hasChild = hasChild;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
	@Cascade(value = { CascadeType.DELETE })
	@OrderBy("priority desc")
	public Set<ArticleCategoryDomain> getChildren() {
		return children;
	}

	public void setChildren(Set<ArticleCategoryDomain> children) {
		this.children = children;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
	@Cascade(value = { CascadeType.DELETE })
	@OrderBy("createDate desc")
	public Set<ArticleDomain> getArticleSet() {
		return articleSet;
	}

	public void setArticleSet(Set<ArticleDomain> articleSet) {
		this.articleSet = articleSet;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
}
